@extends('layouts.console_header')

@section('title', '消费订单列表')

@section('resources')
    @parent
    <style>
        .user_list_search{overflow: hidden;margin: 10px 20px;}
        .user_list_search input{float: right;width: 200px;height: 30px;border: 1px solid #000;}
        .user_list_search .user_list_searchbtn{float: right;height: 30px;margin-left: 10px;width: 50px;background-color: #9955c6;color: #fff;border: none;}
        .user_list_pageouter{overflow: hidden;background-color: #fff;}
        .user_list_page{float: right;overflow: hidden;}
        .user_list_page a{float: left;width: 40px;height: 30px;line-height: 30px;text-align: center;}
        .user_list_page .show{background-color: #eee;border: 1px solid #ccc;}
        .display{text-align: center;background-color: #fff;border-collapse:collapse;}
        .display th{padding: 20px 0;}
        .display #tbody_content{border-top: 1px solid #000;border-bottom: 1px solid #000;font-size: 14px;}
        .display #tbody_content tr:nth-child(2n+1){border-bottom: 1px solid #ddd;background-color: #f9f9f9;}
        .display #tbody_content tr:nth-child(2n){border-bottom: 1px solid #ddd;}
        .display #tbody_content tr td{padding: 2px;}
        .display #tbody_content tr td:nth-child(1){background-color: #f1f1f1;}
        nav{height: 50px;line-height: 50px;}
        nav a,nav span{margin-left: 20px;}
        nav input{margin-left: 20px;height: 28px;border: 1px solid #000;margin-top: 10px;}
        .consume_count span{margin: 10px 0 10px 20px;}
        .refund_prompt{width: 540px;overflow: hidden;z-index: 1001;box-shadow: 0px 0px 5px 5px rgba(128,128,128,0.5);position: fixed;top: 200px;left: 50%;margin-left: -210px;background: #fff;display: none;}
        .refund_prompt .title{overflow: hidden;line-height: 50px;border-bottom: 1px solid #000;}
        .refund_prompt .title span{margin-left: 10px;font-size: 20px;}
        .refund_prompt .title img{margin: 10px;width: 30px;}
        .refund_prompt .content{font-size: 14px;padding: 10px;}
        .refund_prompt .content p{overflow: hidden;padding: 0 40px;}
        .refund_prompt .content p span{float: left;margin-right: 10px;}
        .refund_prompt .content p span:nth-child(1){width: 60px;}
        .refund_prompt .content .money{overflow: hidden;padding: 0 40px;margin: 10px 0;}
        .refund_prompt .content .refund_money{color: red;}
        .refund_prompt .content .red_money{color: red;}
        .refund_prompt .content .refund_remark{overflow: hidden;padding: 0 40px;margin: 10px 0;}
        .refund_prompt .content .money span{float: left;margin-right: 10px;line-height: 30px;}
        .refund_prompt .content .money input{float: left;height: 30px;width: 200px;border: 1px solid #000;border-radius: 5px;padding: 0 5px;}
        .refund_prompt .content .refund_remark span{float: left;margin-right: 10px;line-height: 30px;}
        .refund_prompt .content .refund_remark textarea{float: left;height: 60px;width: 200px;border: 1px solid #000;border-radius: 5px;padding: 5px;}
        .refund_prompt .button{overflow: hidden;padding: 20px;}
        .refund_close,.refund_cancel,.refund_sure{cursor: pointer;}
        .refund_for_sure{width: 420px;overflow: hidden;z-index: 1001;box-shadow: 0px 0px 5px 5px rgba(128,128,128,0.5);position: fixed;top: 70px;left: 50%;margin-left: -210px;background: #fff;display: none;padding: 20px 0;}
        .refund_for_sure .consume_center{margin: 10px 0;text-align: center;}
        .refund_for_sure button{cursor: pointer;}
    </style>
@endsection

@section('content')
<section class="user_list">
    <div style="border-bottom: 2px solid #9955c6;overflow: hidden;">
        <p class="fz_14 account_title fl" style="border: none;">
            <span style="color: #7f7f7f;"><a href="/console/index">首页</a>>订单管理></span>
            <span class="co_violet">消费订单列表</span>
        </p>
        <a href="#" onclick="export_data()"><p class="fr tc back_btn">导出</p></a>
    </div>
    <nav>
        <a class="fl" href="/console/order/consume/list?cityid={{$cityid}}&type=today">今日</a>
        <a class="fl" href="/console/order/consume/list?cityid={{$cityid}}&type=yesterday">昨日</a>
        <a class="fl" href="/console/order/consume/list?cityid={{$cityid}}&type=week">本周</a>
        <a class="fl" href="/console/order/consume/list?cityid={{$cityid}}&type=thismonth">本月</a>
        <a class="fl" href="/console/order/consume/list?cityid={{$cityid}}&type=lastmonth">上月</a>
        <a class="fl" href="/console/order/consume/list?cityid={{$cityid}}&type=all">全部</a>
        <span class="fl">时段选择</span>
        <input class="fl" type="date" name="starttime" value="{{date('Y-m-d', strtotime($starttime))}}" onchange="select_date()">
        <span class="fl">至</span>
        <input class="fl" type="date" name="lasttime" value="{{date('Y-m-d', strtotime($lasttime))}}" onchange="select_date()">
    </nav>
    <p class="user_list_search">
        <input class="user_list_searchbtn" type="button" name="submit_search" value="搜索">
        <input type="text" name="search" value="{{$search}}" placeholder="搜索">
    </p>
    <p class="consume_count oh">
        <span class="fl">合计：{{$total}}笔订单（用户买单：{{$usersweepcount}}/商家扫描：{{$shopsweepcount}}）</span>
        <span class="fl">消费金额：{{$sum}}</span>
        <span class="fl">使用红包：-{{$redenvelopeamount}}</span>
        <span class="fl">实付金额：{{$actualamount}}</span>
        <span class="fl">获得红包：{{$redenvelopebonus}}</span>
    </p>
    <table class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>序号</th>
                <th style="width: 210px;">消费单号</th>
                <th style="width: 110px;">用户昵称</th>
                <th>用户账号</th>
                <th>门店名称</th>
                <th>商家代码</th>
                <th>一级分类</th>
                <th>二级分类</th>
                <th>商务人员</th>
                {{--<th>原商务人员</th>--}}
                <th>买单方式</th>
                <th>消费金额</th>
                <th>使用红包</th>
                <th>实付金额</th>
                <th>获得红包</th>
                <th>订单时间</th>
                <th style="width: 90px;">操作</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>序号</th>
                <th>消费单号</th>
                <th>用户昵称</th>
                <th>用户账号</th>
                <th>门店名称</th>
                <th>商家代码</th>
                <th>一级分类</th>
                <th>二级分类</th>
                <th>商务人员</th>
                {{--<th>原商务人员</th>--}}
                <th>买单方式</th>
                <th>消费金额</th>
                <th>使用红包</th>
                <th>实付金额</th>
                <th>获得红包</th>
                <th>订单时间</th>
                <th>操作</th>
            </tr>
        </tfoot>
        <tbody id="tbody_content">
            @foreach($lists as $list)
                <tr>
                @if(!isset($i))
                    <td>{{$i=($page-1)*$perpage+1}}</td>
                @else
                    <td>{{++$i}}</td>
                @endif
                @if($list['refundstatus'])
                    <td style="overflow: hidden;"><img style="width: 32px;float: left;margin-top: 2px;" src="{{env('IMAGE_DOMAIN')}}images/console/refund.png" alt=""><span style="float: left;line-height: 32px;">{{$list['ordernumber']}}</span></td>
                @else
                    <td>{{$list['ordernumber']}}</td>
                @endif
                    <td>{{$list['nickname']}}</td>
                    <td>{{$list['mobile']}}</td>
                    <td>{{$list['shopname']}}</td>
                    <td>{{$list['code']}}</td>
                    <td>{{$list['firstclassname']}}</td>
                    <td>{{$list['secondclassname']}}</td>
                    <td>{{$list['adminname']}}</td>
                    {{--<td>{{$list['oldmaintainer']}}</td>--}}
                @if($list['path'] == 'user')
                    <td>用户买单</td>
                @elseif($list['path'] == 'shop')
                    <td>商家扫描</td>
                @else
                    <td></td>
                @endif
                    <td>{{$list['orderamount']}}</td>
                @if($list['redenvelopeamount'] == 0.00 || $list['redenvelopeamount'] == 0)
                    <td>--</td>
                @else
                    <td>-{{$list['redenvelopeamount']}}</td>
                @endif
                    <td>{{$list['actualamount']}}</td>
                @if($list['bonus'] == 0.00 || $list['bonus'] == 0)
                    <td>--</td>
                @else
                    <td>{{$list['bonus']}}</td>
                @endif
                    <td>{{date('Y-m-d H:i', strtotime($list['created_at']))}}</td>
                    <td>
                        <a class="co_violet" href="/console/order/consume/details?cityid={{$cityid}}&orderid={{$list['orderid']}}">详情</a>
                        @if($roleid == 1 || $roleid==6)
                            @if($list['refundstatus'] == 0)
                                <a class="refund" href="#" onclick="refund_money('{{$list['ordernumber']}}','{{$list['nickname']}}','{{$list['mobile']}}','{{$list['shopname']}}','{{$list['code']}}','{{$list['orderamount']}}','{{$list['userid']}}')">退款</a>
                            @else
                                <span style="color: #808080;">退款</span>
                            @endif
                        @endif
                    </td>
                </tr>
            @endforeach
        </tbody>
    </table>
    <div class="user_list_pageouter">
        <p class="user_list_page">
            <a href="/console/order/consume/list?cityid={{$cityid}}&page=1&starttime={{date('Y-m-d', strtotime($starttime))}}&lasttime={{date('Y-m-d', strtotime($lasttime))}}&search={{$search}}">首页</a>
            @if($lastpage <=5)
                @for($i=1; $i<=$lastpage; $i++)
                    @if($page == $i)
                        <a href="/console/order/consume/list?cityid={{$cityid}}&page={{$i}}&starttime={{date('Y-m-d', strtotime($starttime))}}&lasttime={{date('Y-m-d', strtotime($lasttime))}}&search={{$search}}" class="show">{{$i}}</a>
                    @else
                        <a href="/console/order/consume/list?cityid={{$cityid}}&page={{$i}}&starttime={{date('Y-m-d', strtotime($starttime))}}&lasttime={{date('Y-m-d', strtotime($lasttime))}}&search={{$search}}">{{$i}}</a>
                    @endif
                @endfor
            @else
                @if($page>2)
                    <a href="/console/order/consume/list?cityid={{$cityid}}&page={{$page-2}}&starttime={{date('Y-m-d', strtotime($starttime))}}&lasttime={{date('Y-m-d', strtotime($lasttime))}}&search={{$search}}">{{$page-2}}</a>
                @endif
                @if($page>1)
                    <a href="/console/order/consume/list?cityid={{$cityid}}&page={{$page-1}}&starttime={{date('Y-m-d', strtotime($starttime))}}&lasttime={{date('Y-m-d', strtotime($lasttime))}}&search={{$search}}">{{$page-1}}</a>
                @endif
                <a href="/console/order/consume/list?cityid={{$cityid}}&page={{$page}}&starttime={{date('Y-m-d', strtotime($starttime))}}&lasttime={{date('Y-m-d', strtotime($lasttime))}}&search={{$search}}" class="show">{{$page}}</a>
                @if(($page+5)<$lastpage)
                    <a href="/console/order/consume/list?cityid={{$cityid}}&page={{$page+1}}&starttime={{date('Y-m-d', strtotime($starttime))}}&lasttime={{date('Y-m-d', strtotime($lasttime))}}&search={{$search}}">{{$page+1}}</a>
                    <a href="/console/order/consume/list?cityid={{$cityid}}&page={{$page+2}}&starttime={{date('Y-m-d', strtotime($starttime))}}&lasttime={{date('Y-m-d', strtotime($lasttime))}}&search={{$search}}">{{$page+2}}</a>
                    <a href="/console/order/consume/list?cityid={{$cityid}}&page={{$page+3}}&starttime={{date('Y-m-d', strtotime($starttime))}}&lasttime={{date('Y-m-d', strtotime($lasttime))}}&search={{$search}}">{{$page+3}}</a>
                    <a href="/console/order/consume/list?cityid={{$cityid}}&page={{$page+4}}&starttime={{date('Y-m-d', strtotime($starttime))}}&lasttime={{date('Y-m-d', strtotime($lasttime))}}&search={{$search}}">{{$page+4}}</a>
                    <a href="">....</a>
                    <a href="/console/order/consume/list?cityid={{$cityid}}&page={{$lastpage}}&starttime={{date('Y-m-d', strtotime($starttime))}}&lasttime={{date('Y-m-d', strtotime($lasttime))}}&search={{$search}}">{{$lastpage}}</a>
                @else
                    @for($i=1; $i<=5;$i++)
                        @if($page+$i<=$lastpage)
                            <a href="/console/order/consume/list?cityid={{$cityid}}&page={{$page+$i}}&starttime={{date('Y-m-d', strtotime($starttime))}}&lasttime={{date('Y-m-d', strtotime($lasttime))}}&search={{$search}}">{{$page+$i}}</a>
                        @endif
                    @endfor
                @endif
            @endif
            <a href="/console/order/consume/list?cityid={{$cityid}}&page={{$lastpage}}&starttime={{date('Y-m-d', strtotime($starttime))}}&lasttime={{date('Y-m-d', strtotime($lasttime))}}&search={{$search}}">末页</a>
        </p>
    </div>
    <!--退款 -->
    <div class="refund_prompt">
        <p class="title">
            <span class="fl">退款</span>
            <img class="fr refund_close" src="{{env('IMAGE_DOMAIN')}}images/close_p.png" alt="">
        </p>
        <div class="content">
            <p>
                <span>消费单号</span>
                <span class="order"></span>
                <input type="hidden" name="ordernumber" value="">
            </p>
            <p>
                <span>用户</span>
                <span class="name"></span>
                <input type="hidden" name="userid" value="">
            </p>
            <p>
                <span>商家</span>
                <span class="shopname"></span>
            </p>
            <p>
                <span>消费金额</span>
                <span class="orderamount"></span>
            </p>
            <p style="color: red;">
                <span>退款金额</span>
                <span class="refund_money"></span>
            </p>
            <p style="color: red;" id="red_money">
                <span>红包变更</span>
                <span class="red_money"></span>
            </p>
            <div class="money">
                <span>退款密码</span>
                <input type="password" name="password" value="">
            </div>
            <div class="refund_remark">
                <span>退款备注</span>
                <textarea name="refundremark" id="" cols="30" rows="10"></textarea>
            </div>
        </div>
        <p class="button">
            <button style="background-color: #9955c6;color: #fff;line-height: 30px;" class="refund_sure fl">确定</button>
            <button style="line-height: 30px;" class="refund_cancel fr">取消</button>
        </p>
    </div>
    <div class="hot_search_mark consume_out_mark" style="z-index: 1001;">
        <p class="oh">
            <span class="fl content">提示</span>
            <img class="fr close" src="{{env('IMAGE_DOMAIN')}}images/close_p.png" alt="">
        </p>
        <p class="consume_center" style="padding: 60px 30px;height: 58px;line-height: 22px;">
            导出数据量较大，可能会导致数据导出失败，建议选择一个月内的数据导出！是否要继续导出？
        </p>
        <p>
            <button style="background-color: #9955c6;color: #fff;line-height: 30px;" class="sure fl">继续</button>
            <button style="line-height: 30px;" class="cancel fr">取消</button>
        </p>
    </div>
    <div class="refund_for_sure">
        <p class="consume_center">
            确认做退款处理吗？
        </p>
        <p>
            <button style="background-color: #9955c6;color: #fff;line-height: 30px;width: 100px;height: 30px;" class="refund_for_sure_sure fl">确定</button>
            <button style="line-height: 30px;width: 100px;height: 30px;" class="refund_for_sure_cancel fr">取消</button>
        </p>
    </div>
</section>
@endsection

@section('scriptResources')
    @parent
    <script>
    var cityid='{{$cityid}}';
    var starttime='{{$starttime}}';
    var lasttime='{{$lasttime}}';
    var str = window.location.href;
    if(str.indexOf("today") > 0){
        $("nav a").eq(0).css({"color": "#9955c6"});
    }else if(str.indexOf("yesterday") > 0){
        $("nav a").eq(1).css({"color": "#9955c6"});
    }else if(str.indexOf("week") > 0){
        $("nav a").eq(2).css({"color": "#9955c6"});
    }else if(str.indexOf("thismonth") > 0){
        $("nav a").eq(3).css({"color": "#9955c6"});
    }else if(str.indexOf("lastmonth") > 0){
        $("nav a").eq(4).css({"color": "#9955c6"});
    }else if(str.indexOf("all") > 0){
        $("nav a").eq(5).css({"color": "#9955c6"});
    }
    function select_date() {
        var startTime = $("input[name='starttime']").val();
        var lastTime = $("input[name='lasttime']").val();
        if(!startTime || !lastTime) {
            alert('请选择开始时间和结束时间'); return;
        }
        window.location.href="/console/order/consume/list?cityid=" + cityid+"&starttime="+startTime+"&lasttime="+lastTime;
    }
    function export_data() {
        var startTime = $("input[name='starttime']").val();
        var lastTime = $("input[name='lasttime']").val();
        var search = $("input[name='search']").val();
        var start_date = new Date(startTime);
        var last_date = new Date(lastTime);
        var differ = last_date.getTime()-start_date.getTime();
        differ = parseInt(differ/(1000*60*60*24));
        if(!startTime || !lastTime) {
            alert('请选择开始时间和结束时间'); return;
        }
        if(differ > 30){
            $(".consume_out_mark").show();
            $(".public_prompt_mark").show();
            $(".public_prompt_mark").height($(window).height());
            $(".consume_out_mark .sure").click(function(){
                window.open("/console/order/consume/export?cityid="+cityid+"&search="+search+"&starttime="+startTime+"&lasttime="+lastTime, '_blank');
                $(".consume_out_mark").hide();
                $(".public_prompt_mark").hide();
            });
            $(".consume_out_mark .cancel").click(function(){
                $(".consume_out_mark").hide();
                $(".public_prompt_mark").hide();
            });
            $(".consume_out_mark .close").click(function(){
                $(".consume_out_mark").hide();
                $(".public_prompt_mark").hide();
            });
        }else{
            window.open("/console/order/consume/export?cityid="+cityid+"&search="+search+"&starttime="+startTime+"&lasttime="+lastTime, '_blank');
        }
    }

    $(".user_list_searchbtn").click(function() {
        var search=$("input[name='search']").val();
        window.location.href = "/console/order/consume/list?cityid="+cityid+"&search="+search+"&starttime="+starttime+"&lasttime="+lasttime+"&now=" + Date.parse(new Date());
    });
    //退款
    function refund_money(ordernumber,nickname,mobile,shopname,code,orderamount,userid) {
        $(".refund_prompt").show();
        $(".public_prompt_mark").show();
        $(".public_prompt_mark").height($(window).height());
        $(".refund_prompt .order").html(ordernumber);
        $(".refund_prompt input[name='ordernumber']").val(ordernumber);
        $(".refund_prompt .name").html(nickname + "（" + mobile + "）");
        $(".refund_prompt .shopname").html(shopname + "（ "+ code + "）");
        $(".refund_prompt .orderamount").html(orderamount);
        $.ajax({
            url : '/console/order/consume/refund/getmoney',
            type : 'get',
            data: {
                'userid':userid,
                'ordernumber':ordernumber,
                'cityid':cityid
            },
            dataType:'json',
            success:function(data){
                if(data.status == 'success') {
                    var data = data.data;
                    var refundmoney = data['refundmoney'];
                    var redenvelopeinfo = data['redenvelopeinfo'];
                    console.log(redenvelopeinfo);
                    if(redenvelopeinfo == ''){
                        $(".refund_prompt #red_money").hide();
                    }else {
                        $(".refund_prompt .red_money").html(redenvelopeinfo);
                    }
                    $(".refund_prompt .refund_money").html(refundmoney);
                } else {
                    //alert(data.message);
                    $(".refund_prompt .refund_money").html("获取金额失败");
                    $(".refund_prompt .red_money").html("无");
                }
            }
        });
    }
    $(".refund_sure").on("click",function () {
        $(".refund_for_sure").show();
        $(".refund_prompt").css('z-index','1');
    })
    $(".refund_for_sure_sure").on("click",function () {
        $(".refund_for_sure").css('z-index','1');
        var userid = $(".refund_prompt input[name='userid']").val();
        var ordernumber = $(".refund_prompt input[name='ordernumber']").val();
        var password = $(".refund_prompt input[name='password']").val();
        var refundremark = $(".refund_prompt textarea[name='refundremark']").val();
        $.ajax({
            url : '/console/order/consume/refund/submit',
            type : 'post',
            data: {
                'userid' : userid,
                'ordernumber': ordernumber,
                'cityid':cityid,
                'password':password,
                'refundremark':refundremark
            },
            dataType:'json',
            success:function(data){
                if(data.status == 'success') {
                    alert(data.message);
                    window.location.reload();
                } else {
                    alert(data.message);
                    $(".refund_for_sure").css('z-index','1003');
                }
            }
        });
    })
    $(".refund_close").on("click",function () {
        $(".refund_prompt").hide();
        $(".public_prompt_mark").hide();
    })
    $(".refund_cancel").on("click",function () {
        $(".refund_prompt").hide();
        $(".public_prompt_mark").hide();
    })
    $(".refund_for_sure_cancel").on("click",function () {
        $(".refund_for_sure").hide();
        $(".refund_prompt").css('z-index','1001');
    })
    </script>
@endsection